import { useMysql } from "../mysql/index.js";
import { MysqlQueryBuilder } from "../mysql/mysql-query-builder.js";
// 查询所有事件数据
export const getEventAll = async (req, res) => {
  const sql = MysqlQueryBuilder.select("events");
  try {
    const result = await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "查询成功",
      data: result,
    });
  } catch (err) {
    console.error("查询失败:", err);
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 分页查询事件
export const getEventArr = async (req, res) => {
  const { page, pageSize, orderBy } = req.body;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "events",
    ["*"],
    {},
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 更新事件
export const updataEventById = async (req, res) => {
  const { event_id, event_type_id, description, type_level, risk_level } =
    req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.update(
    "events",
    {
      event_id: event_id,
      event_type_id: event_type_id,
      description: description,
      type_level: type_level,
      risk_level: risk_level,
    },
    {
      event_id: event_id,
    }
  );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "修改成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 删除事件
export const deleteEventById = async (req, res) => {
  const id = req.params.id;
  const sql = MysqlQueryBuilder.delete("events", {
    event_id: id,
  });
  try {
    const result = await useMysql(sql); // 执行SQL
    res.status(200).json({
      code: 200,
      message: "删除成功",
    });
  } catch (err) {
    res.status(500).json({
      code: 500,
      message: "服务器内部错误",
      error: err.message,
    });
  }
};
// 删除事件(多id批量)
export const deleteEventMoreById = async (req, res) => {
  const ids = req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.delete("events", {
    event_id: ids,
  });
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "删除成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 查找事件(单条件)
export const SearchEventKey = async (req, res) => {
  const { SearchKey, SearchValue, page, pageSize, orderBy } = req.body;
  // 中间转化
  const TemObject = {};
  TemObject[SearchKey] = SearchValue;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "events",
    ["*"],
    TemObject,
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    const totalPages = Math.ceil(total / pageSize);
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 添加事件
export const addEvent = async (req, res) => {
  const { event_id, event_type_id, description, type_level, risk_level } =
    req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.insert(
    "events",
    {
      event_id: event_id,
      event_type_id: event_type_id,
      description: description,
      type_level: type_level,
      risk_level: risk_level,
    },
    {
      event_id: event_id,
    }
  );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "添加成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};

// 查询操作列表(事件类型)
export const typeList = async (req, res) => {
    const sql = MysqlQueryBuilder.select("event_types");
    try {
      const result = await useMysql(sql);
      res.status(200).send({
        code: 200,
        message: "查询成功",
        data: result,
      });
    } catch (err) {
      console.error("查询失败:", err);
      res.status(500).send({
        code: 500,
        message: "服务器内部错误",
      });
    }
  }


// 分页查询事件大类
export const getBigEventArr = async (req, res) => {
  const { page, pageSize, orderBy } = req.body;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "event_types",
    ["*"],
    {},
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 查找事件(单条件)
export const SearchEventTypeKey = async (req, res) => {
  const { SearchKey, SearchValue, page, pageSize, orderBy } = req.body;
  // 中间转化
  const TemObject = {};
  TemObject[SearchKey] = SearchValue;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "event_types",
    ["*"],
    TemObject,
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    const totalPages = Math.ceil(total / pageSize);
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 更新指定事件大类
export const updataEventTypeById = async (req, res) => {
  const { event_type_id, event_type_name, group_type } =
    req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.update(
    "event_types",
    {
      event_type_id: event_type_id,
      event_type_name: event_type_name,
      group_type: group_type,
    },
    {
      event_type_id: event_type_id,
    }
  );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "修改成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};